/**
 * Created by Administrator on 2017/3/21.
 */
import React, {Component} from 'react';
import {Form, Input, Radio, Select,   Row,Checkbox,Col,
  Upload, } from 'antd';
import {connect} from 'dva';
import moment from 'moment'
import request from '@/utils/request';
const FormItem = Form.Item;
const { TextArea } = Input;
const RadioGroup = Radio.Group;
const Option = Select.Option;
class AddPoliciesForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      users:[]
    };
  }

  componentDidMount() {
    const that=this;
    request(`/all_users`, {
      method: 'GET',
      params:{
        per_page:500
      }
    }).then((response)=> {
      if(response.status===200){
        that.setState({
          users:response.data.data
        })
      }
    })
  }
  render() {
    const formItemLayoutWithLabel = {
      labelCol: {
        xs: {span: 24},
        sm: {span: 6},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 15},
      }
    };

    const {getFieldDecorator} = this.props.form;
    const children = [];
    for (let i = 0; i < this.state.users.length; i++) {
      children.push(<Option key={this.state.users[i].id}>{this.state.users[i].username}</Option>);
    }
    return (
      <div>
        <Form onSubmit={this.handleSubmit}>
          <FormItem
            {...formItemLayoutWithLabel}
            label={(
              <span>
               微信公众号报警通知
            </span>
            )}
          >
            {getFieldDecorator('wechat_alarm', {
              initialValue: this.props.editRecord?this.props.editRecord.wechat_alarm:1,
              rules: [{ required: true, message: '请选择微信公众号报警通知' }],
            })(
             <Select>
               <Option value={1}>通知</Option>
               <Option value={-1}>不通知</Option>
             </Select>
            )}
          </FormItem>
          <FormItem
            {...formItemLayoutWithLabel}
            label={(
              <span>
               网页弹窗报警通知
            </span>
            )}
          >
            {getFieldDecorator('website_alarm', {
              initialValue: this.props.editRecord?this.props.editRecord.website_alarm:1,
              rules: [{ required: true, message: '请选择网页弹窗报警通知' }],
            })(
              <Select>
                <Option value={1}>通知</Option>
                <Option value={-1}>不通知</Option>
              </Select>
            )}
          </FormItem>
          <FormItem
            {...formItemLayoutWithLabel}
            label={(
              <span>
               选择报警用户
            </span>
            )}
            help={'请先关注天源公众号及在小程序内绑定用户'}
          >
            {getFieldDecorator('administrators_arr', {
              initialValue: this.props.editRecord?this.props.editRecord.administrators.map((item,index)=>item.id):"",
              rules: [{ required: true, message: '请选择报警用户' }],
            })(
              <Select
                mode="multiple"
                style={{width: '100%'}}
                placeholder="选择用户"
              >

                {children}
              </Select>
            )}
          </FormItem>
          <FormItem
            {...formItemLayoutWithLabel}
            label={(
              <span>
               微信报警间隔
            </span>
            )}
            help={'报警间隔，单位：分钟，最小30，0表示不开启微信报警'}
          >
            {getFieldDecorator('alarm_interval', {
              initialValue: this.props.editRecord?this.props.editRecord.alarm_interval:"30",
              rules: [{ required: true, message: '请输入报警间隔' }],
            })(
              <Input />
            )}
          </FormItem>
        </Form>
      </div>
    );
  }
}

const AddPoliciesFormWrap = Form.create()(AddPoliciesForm);
export default connect()(AddPoliciesFormWrap);


